/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package cn.eclass.bean;

import java.util.ArrayList;
import java.util.List;

import cn.eclass.bean.api.ICategoryBean;
import cn.eclass.entity.CategoryEntity;
//test commit

class RootCatEntity extends CategoryEntity {

    @Override
    public int getId() {
        return 0;
    }

    @Override
    public void setId(int id) {
    }

    @Override
    public String getName() {
        return "root";
    }

    @Override
    public void setName(String name) {
    }

    @Override
    public int getParentId() {
        return -1;
    }

    @Override
    public void setParentId(int parentId) {
    }

    @Override
    public String getDescription() {
        return "root";
    }

    @Override
    public void setDescription(String description) {
    }

    @Override
    public CategoryEntity getParent() {
        return null;
    }

    @Override
    public void setParent(CategoryEntity parent) {
    }
}

/**
 *
 * @author Administrator
 */
public class CategoryBean implements ICategoryBean {

    private static CategoryEntity root = null;
    cn.eclass.dao.CategoryDao categoryDao = new cn.eclass.dao.CategoryDao();

    public CategoryEntity getById(int id) {
        return categoryDao.getById(id);
    }

    //all sub lists will be appended to list.
    public void getListFromTree(int catId, List<Integer> list) {
        CategoryEntity cat = getByIdInTree(catId);
        List<CategoryEntity> children = cat.getChildren();

        if (list == null) {
            list = new ArrayList<Integer>();
        }

        if (children == null || children.size() == 0) {
            list.add(cat.getId());
            return;
        }

        for (CategoryEntity child : children) {
            getListFromTree(child.getId(), list);
        }
        list.add(cat.getId());
    }

    public CategoryEntity getByIdInTree(int id) {
        return findByIdHelper(root, id);
    }

    private CategoryEntity findByIdHelper(CategoryEntity cat, int id) {
        if (cat.getId() == id) {
            return cat;
        }
        List<CategoryEntity> childList = cat.getChildren();
        if (childList == null) {
            return null;
        } else {
            for (CategoryEntity childCat : childList) {
                CategoryEntity res = findByIdHelper(childCat, id);
                if (res != null) {
                    return res;
                }
            }
        }
        return null;

    }

    public CategoryEntity getByIdIndeep(int id) {
        CategoryEntity category = categoryDao.getById(id);
        if (null != category) {
            category.setChildren(categoryDao.getByParentId(category.getId()));
            category.setParent(categoryDao.getById(category.getParentId()));
        }
        return category;
    }

    private void loadCatTree(CategoryEntity cat) {
        int id = cat.getId();
        List<CategoryEntity> list = categoryDao.getByParentId(id);
        if (list == null) {
            return;
        } else {
            cat.setChildren(list);
            for (CategoryEntity kidCat : list) {
                //kidCat.setParent(cat);
                loadCatTree(kidCat);
            }
        }
    }

    public void loadCatTree() {
        System.out.println("loading tree");
        loadCatTree(root);
        System.out.println("tree loaded");
    }

//    @Override
//    public int getItemCount() {
//    	if()
//    }
    public List<CategoryEntity> getByParentId(int parentId) {
        return categoryDao.getByParentId(parentId);
    }

    public List<CategoryEntity> getList(/*
             * Pagination
             */) {
        return categoryDao.getList(/*
                 * Pagination
                 */);
    }

    public void update(CategoryEntity category) {
        categoryDao.update(category);
    }

    public CategoryEntity insert(CategoryEntity categoryEntity) {
        return categoryDao.insert(categoryEntity);
    }

    public void remove(Integer id) {
        categoryDao.remove(id);
    }

    @Override
    public CategoryEntity getRoot() {
        if (root == null) {
            root = new RootCatEntity();
            loadCatTree();
        }
        return root;
    }

    @Override
    public CategoryEntity getNewRoot() {
        root = new RootCatEntity();
        loadCatTree();
        return root;
    }

    @Override
    public CategoryEntity getByName(String search) {
        return categoryDao.getByName(search);
    }
}
